﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.Classes;

namespace IndexMarkSilverlightClient.Views.Controls
{
    public partial class ISTreeView : UserControl, INotifyPropertyChanged
    {
        IndexMarkServiceClient client = null;

        private ObservableCollection<Section> secTree = new ObservableCollection<Section>();

        public ObservableCollection<Section> SecTree
        {
            get
            {
                return secTree;
            }
            set
            {
                secTree = value;
            }
        }

        public event EventHandler LoadCompleted;

        public void InvokeLoadCompleted(EventArgs e)
        {
            EventHandler handler = LoadCompleted;
            if (handler != null) handler(this, e);
        }

        public IEnumerable<Characteristic> Characteristics
        {
            get 
            {
                List<Characteristic> list = new List<Characteristic>();
                foreach (Section Sec in secTree)
                {
                    list.AddRange(GetCharsFromSec(Sec));
                }
                return list.Where(x => x.Check).ToList(); 
                
            }
        }

        public IEnumerable<Section> Sections
        {
            get
            {
                List<Section> list = new List<Section>();
                foreach (Section Sec in secTree)
                {
                    list.AddRange(GetSecFromSec(Sec));
                }
                return list.Where(x => x.Check).ToList();
            }
        }

        public int IDTerm { get; set; }

        private IEnumerable<Characteristic> GetCharsFromSec(Section Sec)
        {
            List<Characteristic> list = new List<Characteristic>();
            foreach (var Obj in Sec.Childs)
            {
                if (Obj is Characteristic)
                {
                    list.Add(Obj as Characteristic);
                }
                if (Obj is Section)
                {
                    list.AddRange(GetCharsFromSec(Obj as Section));
                }
            }
            return list;
        }

        private IEnumerable<Section> GetSecFromSec(Section Sec)
        {
            List<Section> list = new List<Section>();
            list.Add(Sec);
            foreach (var Obj in Sec.Childs)
            {
                if (Obj is Section)
                {
                    list.AddRange(GetSecFromSec(Obj as Section));
                }
            }
            return list;
        }
        
        public ISTreeView()
        {
            InitializeComponent();
            client = new IndexMarkServiceClient();
            client.GetISTreeCompleted += new EventHandler<GetISTreeCompletedEventArgs>(client_GetISTreeCompleted);
            client.GetTreeCompleted += new EventHandler<GetTreeCompletedEventArgs>(client_GetTreeCompleted);
          // Loaded += new RoutedEventHandler(ISTreeView_Loaded);
            
        }

        void client_GetTreeCompleted(object sender, GetTreeCompletedEventArgs e)
        {
            secTree.Clear();
            foreach (Section D in e.Result)
            {
                D.ImagePath = ImageRepository.Instance.SetTreeItemImage(D);
                TraversTree(D as Section);
                D.Check = true;
                CheckTree(D,true);
                secTree.Add(D as Section);

               
            }
            this.DataContext = secTree;


            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Sections"));
            }
            PropertyChangedInit("secTree");
            InvokeLoadCompleted(EventArgs.Empty);
        }

        void ISTreeView_Loaded(object sender, RoutedEventArgs e)
        {
            LoadTree();
        }

        public void LoadTree()
        {
            //int IDTerm = ((((this.Parent as TabItem).Parent as TabControl).Parent as Grid).Parent as IndexSettings).CurrentTermID;
            client.GetTreeAsync(IDTerm);
        }

        public void HideChars()
        {
            foreach (Section D in this.secTree)
            {
                HideCharsRecursion(D);
            }
        }

        private void HideCharsRecursion(Section Sec)
        {
            for (int i = 0; i < Sec.Childs.Count; i++)
            {
                var O = Sec.Childs[i];
                if (O is Characteristic)
                {
                    Sec.Childs.RemoveAt(i);
                }
                if (O is Section)
                {
                    HideCharsRecursion(O as Section);
                }
            }
        }

        void client_GetISTreeCompleted(object sender, GetISTreeCompletedEventArgs e)
        {
            secTree.Clear();
            foreach (Section D in e.Result)
            {
                D.ImagePath = ImageRepository.Instance.SetTreeItemImage(D);
                TraversTree(D as Section);
                secTree.Add(D as Section);
            }
            this.DataContext = secTree;

            

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("secTree"));
            }
            PropertyChangedInit("secTree");
            InvokeLoadCompleted(EventArgs.Empty);
        }

        private void TraversTree(Section S)
        {
            S.ImagePath = ImageRepository.Instance.SetTreeItemImage(S);
            foreach (var TI in S.Childs)
            {
                TI.ImagePath = ImageRepository.Instance.SetTreeItemImage(TI);
                if (TI is Section)
                {
                    TraversTree(TI as Section);
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void CheckTree(Section S, bool flag)
        {
            S.Check = flag;
            foreach (var obj in S.Childs)
            {
                if (obj is Section)
                {
                    (obj as Section).Check = flag;
                    CheckTree(obj as Section, flag);
                }
                else
                {
                    (obj as Characteristic).Check = flag;
                }
            }
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (ISTree.SelectedItem is Section)
            {
                CheckTree(ISTree.SelectedItem as Section, (ISTree.SelectedItem as Section).Check);
            }
            PropertyChangedInit("secTree");
        }

        private void PropertyChangedInit(String PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}
